<template>
  <div
    class="queryInfo"
    ref="containerRef"
    :id="'queryInfo-' + (infoContainerShow ? 'show' : 'hiden')"
  >
    <!-- 顶端标题&控制按钮 -->
    <div
      class="infoTitle"
      :id="'infoTitle-' + (infoContainerShow ? 'show' : 'hiden')"
    >
      <!-- 标题 -->
      <span v-show="infoContainerShow" style="minwidth: 350px">查询结果</span>
      <!-- 隐藏 -->
      <img
        src="@/assets/images/icon/infoList/隐藏.png"
        @click="infoContainerShow = !infoContainerShow"
      />
    </div>
    <!-- 内容列表 -->
    <div class="infoListContainer" v-show="infoContainerShow">
      <!-- 内容列表-左 -->
      <div
        class="infoContainer"
        :style="{ height: infoContainerShow ? '' : '0px' }"
      >
        <el-collapse
          v-show="infoContainerShow"
          v-model="collapseRefLeft"
          class="infoList"
          v-for="(infoList, index) in queryInfoDataLeft"
          :key="index"
        >
          <el-collapse-item :name="index">
            <!-- 标题 -->
            <template #title>
              <div class="resultTitle">
                <img
                  :src="
                    require('@/assets/images/icon/infoList/' +
                      infoList.title +
                      '.png')
                  "
                />
                <span>{{ infoList.title }}</span>
                <span class="resultLength"
                  >{{ infoList.listLength || infoList.list.length }}条</span
                >
                <div class="seeAllData" v-show="index % 2 != 0">
                  <div @click.stop="seeAllData(infoList)">
                    <img src="@/assets/images/icon/selectResult/查看全部.png" />
                    <div class="text">
                      {{ !infoList.seeAllflag ? "查看全部" : "关闭显示" }}
                    </div>
                  </div>
                </div>
              </div>
              <info-filled />
            </template>
            <!-- 信息列表 -->
            <div
              class="infoListContainer"
              :style="{ maxHeight: infoListHeightLeft }"
            >
              <div
                class="info"
                v-for="(info, index2) in infoList.list"
                :key="index2"
                @click="showInfoDetail(info, true, infoList.type)"
              >
                <div class="infoBG"></div>
                <span>{{ info.attributes.Name || info.attributes.NAME }}</span>
              </div>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
      <!-- 内容列表-右 -->
      <div
        class="infoContainer"
        :style="{ height: infoContainerShow ? '' : '0px' }"
      >
        <el-collapse
          v-show="infoContainerShow"
          v-model="collapseRefRight"
          class="infoList"
          v-for="(infoList, index) in queryInfoDataRight"
          :key="index"
        >
          <el-collapse-item :name="index">
            <!-- 标题 -->
            <template #title>
              <div class="resultTitle">
                <img
                  :src="
                    require('@/assets/images/icon/infoList/' +
                      infoList.title +
                      '.png')
                  "
                />
                <span>{{ infoList.title }}</span>
                <span class="resultLength"
                  >{{ infoList.listLength || infoList.list.length }}条</span
                >
                <div class="seeAllData" v-show="index % 2 != 0">
                  <div @click.stop="seeAllData(infoList)">
                    <img src="@/assets/images/icon/selectResult/查看全部.png" />
                    <div class="text">
                      {{ !infoList.seeAllflag ? "查看全部" : "关闭显示" }}
                    </div>
                  </div>
                </div>
              </div>
              <info-filled />
            </template>
            <!-- 信息列表 -->
            <div
              class="infoListContainer"
              :style="{ maxHeight: infoListHeightRight }"
            >
              <div
                class="info"
                v-for="(info, index2) in infoList.list"
                :key="index2"
                @click="showInfoDetail(info, true, infoList.type)"
              >
                <div class="infoBG"></div>
                <span>{{
                  info.attributes.qymc ||
                  info.attributes.Name ||
                  info.attributes.NAME
                }}</span>
              </div>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </div>
  </div>
</template>

<script>
/*eslint-disable*/
import { getCurrentInstance, onMounted, ref, watch } from "vue";
import pubsub from "pubsub-js";
import { useRoute, useRouter, onBeforeRouteEnter } from "vue-router";
import { useStore } from "vuex";
import Message from "@/components/Message/showMessage";

export default {
  name: "查询结果信息列表",
  data() {
    return {
      infoContainerShow: true,
      queryInfoDataLeft: {},
      queryInfoDataRight: {},
      infoListHeightLeft: 0,
      infoListHeightRight: 0,
    };
  },
  props: {
    queryInfoDataProp: {
      type: Object,
      default: {},
    },
  },
  setup() {
    const { proxy } = getCurrentInstance();
    let collapseRefLeft = ref([]);
    let collapseRefRight = ref([]);
    const containerRef = ref(null);
    const route = useRoute();
    const store = useStore();

    watch(
      // 路由更新监听
      () => route.params,
      (newParams) => {
        // console.log(newParams);
        if (isEmptyObject(newParams)) {
          return;
        } else {
          // 信息填入
          infoListHandle();
        }
      }
    );

    onMounted(() => {
      // 地图样式更改
      pubsub.publish("mapExpand", "dataStyle");
      // 数据处理
      infoListHandle();
      // 元素高度计算
      proxy.infoListHeightLeft = itemHeightCaclulate(proxy.queryInfoDataLeft);
      proxy.infoListHeightRight = itemHeightCaclulate(proxy.queryInfoDataRight);
      // 列表展开
      collapseShow();
    });

    // 数据处理
    function infoListHandle() {
      let resultList = [];
      // 是否缺失路由数据
      if (route.params.queryInfoDataProp === undefined) {
        // 读取vuex中的数据
        resultList = store.state.queryInfoList;
      } else {
        // 读取路由数据
        resultList = JSON.parse(route.params.queryInfoDataProp);
        // 数据另存
        store.commit("writeQueryInfoList", resultList);
      }
      console.log(resultList);

      // 数据填入
      proxy.queryInfoDataLeft = resultList.slice(0, 2);
      proxy.queryInfoDataRight = resultList.slice(2);
    }

    // 元素高度计算
    function itemHeightCaclulate(contentList) {
      // 需要分配高度的元素个数
      let needAverageNum = 0;
      // 空余高度
      let surplusHeight = 0;
      // 容器标准总高度
      let containerStandrdHeight =
        ((containerRef.value.offsetHeight - 50 * (contentList.length + 1)) /
          contentList.length) *
        0.98;
      // 遍历比对容纳情况
      contentList.forEach((element) => {
        // 单个容器实际高度
        let containerActualHeight = element.list.length * 40;
        // 空余高度记录
        if (containerActualHeight < containerStandrdHeight) {
          surplusHeight += containerStandrdHeight - containerActualHeight;
        } else {
          needAverageNum++;
        }
      });
      // console.log(needAverageNum, surplusHeight);

      // 平均分配空余高度
      return containerStandrdHeight + surplusHeight / needAverageNum + "px";
    }

    // collaspe展开
    function collapseShow() {
      setTimeout(() => {
        for (let index = 0; index < proxy.queryInfoDataLeft.length; index++) {
          collapseRefLeft.value.push(index);
        }
        for (let index = 0; index < proxy.queryInfoDataRight.length; index++) {
          collapseRefRight.value.push(index);
        }
      }, 500);
    }

    // 验证空对象
    function isEmptyObject(obj) {
      return Object.keys(obj).length === 0 && obj.constructor === Object;
    }

    // 信息点击进入详情
    function showInfoDetail(info, flag, type) {
      pubsub.publish("infoDetailShow", { flag, info, type });
    }

    // 清空搜索结果
    function infoDrawClear() {
      // 清空数据集
      proxy.queryInfoDataLeft = {};
      proxy.queryInfoDataRight = {};
      // 清空图上标注
      pubsub.publish("infoDrawClear");
    }

    // 数据总览
    function seeAllData(data) {
      // 标识更改
      data.seeAllflag = !data.seeAllflag;
      // 信息传递
      pubsub.publish("seeAllData", data);
    }

    return {
      collapseRefLeft,
      collapseRefRight,
      showInfoDetail,
      infoDrawClear,
      seeAllData,
      containerRef,
    };
  },
  watch: {
    infoContainerShow(newValue) {
      pubsub.publish("mapExpand", newValue ? "dataStyle" : "mapStyle");
    },
  },
};
</script>

<style lang="less" scoped>
.queryInfo {
  position: absolute;
  z-index: 999;
  box-shadow: 2px 2px 4px 1px rgba(0, 0, 0, 0.2);
  height: 99%;
  border-radius: 5px;
  transition: width 0.3s ease;

  > .infoTitle {
    height: 50px;
    background-color: white;
    border-top-right-radius: 5px;
    border-top-left-radius: 5px;
    border-bottom: transparent;
    display: flex;
    align-items: center;
    > span {
      color: #373737;
      font-weight: bold;
      font-size: 170%;
      font-family: "方正小标宋简体";
    }
    > .hidenText {
      text-align: center;
    }
    > img {
      width: 25px;
      transition: width 0.3s ease;
    }
    > img:hover {
      cursor: pointer;
      width: 28px;
    }
  }

  > #infoTitle-show {
    justify-content: space-between;
    border-bottom-left-radius: 0px;
    border-bottom-left-radius: 0px;
    flex-direction: row;
    padding: 1%;
    > img {
      transform: rotate(0deg);
    }
  }

  > #infoTitle-hiden {
    height: 100%;
    justify-content: center;
    border-bottom-left-radius: 5px;
    border-bottom-left-radius: 5px;
    // flex-direction: column;
    // padding: 5% 1%;
    > img {
      transform: rotate(-180deg);
    }
  }

  > .infoListContainer {
    display: flex;
    align-items: top;
    justify-content: center;
    flex-direction: row;
    > .infoContainer {
      width: 49%;
      min-width: 500px;
      border-bottom-right-radius: 5px;
      border-bottom-left-radius: 5px;
      border-top: transparent;
      position: relative;
      margin: 0 0.5%;
      padding: 5px;
      display: flex;
      flex-direction: column;
      background-color: white;
      // border-radius: 5px;
      // box-shadow: 2px 2px 5px 1px rgba(0, 0, 0, 0.1);
    }
  }
}

[name="full-height"] {
  flex: 1;
}

#queryInfo-show {
  right: 10px;
  width: 49.25%;
  border: #373737 1px solid;
}

#queryInfo-hiden {
  border: "";
  width: 50px;
  right: 0px;
  box-shadow: 2px 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.resultTitle {
  display: flex;
  align-items: center;
  width: 100%;
  font-family: "黑体";
  color: #3f69e4;
  font-size: 100%;
  font-weight: bold;
  > * {
    margin: 0 0.5%;
  }
  > img {
    width: 20px;
  }
  > .resultLength {
    color: #000;
    font-weight: normal;
    font-size: 80%;
  }
  > .seeAllData {
    color: white;
    font-weight: normal;
    display: flex;
    align-items: center;
    > div {
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: background-color 0.5s ease;
      > img {
        border-radius: 5px;
        width: 25px;
        background-color: #3f69e4;
        padding: 5px;
      }
      > .text {
        font-size: 80%;
        padding-right: 5px;
      }
    }
    > div:hover {
      background-color: #3f69e4;
    }
  }
}

.infoListContainer {
  // max-height: 230px;
  overflow-y: auto;
  overflow-x: hidden;
}

.infoListContainer::-webkit-scrollbar {
  width: 2px;
}

.infoListContainer::-webkit-scrollbar-thumb {
  height: 25px;
  border-radius: 10px;
  background: #3f69e4;
}

.infoListContainer::-webkit-scrollbar-track {
  background: transparent;
}

.info {
  padding: 5px 0;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: black;
  position: relative;
  font-weight: normal;
  transition: font-weight 0.3s ease;
  > span {
    display: inline-block;
    background-color: transparent;
    position: absolute;
    z-index: 999;
    line-height: 120%;
  }
  > .infoBG {
    background-color: rgb(249, 249, 249);
    width: 0px;
    height: 35px;
    position: absolute;
    z-index: 0;
    transition: width 0.3s ease;
  }
}

.info:hover {
  color: #3f69e4;
  font-weight: bold;
  cursor: pointer;
}

.info:hover > .infoBG {
  width: 100%;
}
</style>